package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.BillDetailDao;
import com.juyu.miliao.api.dao.mysql.UserWalletDao;
import com.juyu.miliao.api.dao.redis.FruitMachineRedisDao;
import com.juyu.miliao.api.dao.redis.WinRecordFruitMachineInfoRedisDao;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.BillDetail;
import com.juyu.miliao.api.domain.Gift;
import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.api.domain.WinRecordFruitMachineInfo;
import com.juyu.miliao.api.domain.dto.FruitMachineDTO;
import com.juyu.miliao.api.domain.dto.WinInfoDTO;
import com.juyu.miliao.api.domain.vo.WinAnnouncementVo;
import com.juyu.miliao.common.constant.RecordTypeEnum;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 水果机service
 *
 * @author xuanqicheng
 * @create 2019-15-13:48
 */
@Service
public class FruitMachineService {

    @Autowired
    private UserWalletDao userWalletDao;

    @Autowired
    private BillDetailDao billDetailDao;

    @Autowired
    private FruitMachineRedisDao fruitMachineRedisDao;

    @Autowired
    private WinRecordFruitMachineInfoRedisDao winRecordFruitMachineInfoRedisDao;



    /**
     * 抽奖
     * @param fruitMachineDTO
     * @param userId
     * @param gift
     * @return
     */
    public WinInfoDTO getLuckDrawInfo(FruitMachineDTO fruitMachineDTO, long userId, Gift gift) {

        WinInfoDTO  resultWinInfoDTO= new WinInfoDTO()
                .setStatus(2)
                .setGiftId(gift.getGiftId())
                .setIcon(gift.getIcon())
                .setGiftName(gift.getGiftName());

        int numberOfGoldCoinsSpent = gift.getPrice()*fruitMachineDTO.getMultiple();

        UserWallet userWallet =userWalletDao.getUserWalletById(userId);

        if(userWallet.getDeposit() >= numberOfGoldCoinsSpent){
            //减金币形成账单
            getLuckDrawBill(numberOfGoldCoinsSpent,userId,RecordTypeEnum.FRUIT_MACHINE_CONSUMPTION.state);
            for(int i=0;i<fruitMachineDTO.getMultiple();i++){

                WinInfoDTO winInfoDTO=fruitMachineRedisDao.getLuckDraw(gift);
                /**
                 * 中奖
                 */
                if(winInfoDTO.getStatus()==1){
                    //获取信息入库
                    resultWinInfoDTO
                            .setStatus(winInfoDTO.getStatus())
                            .setGold(resultWinInfoDTO.getGold()+winInfoDTO.getGold())
                            .setMultiple(resultWinInfoDTO.getMultiple()+winInfoDTO.getMultiple());
                }
            }

            if(resultWinInfoDTO.getStatus()==1){
                //增加金币
                getLuckDrawBill(resultWinInfoDTO.getGold(),userId,RecordTypeEnum.FRUIT_MACHINE_EARNING.state);

                WinRecordFruitMachineInfo winRecordFruitMachineInfo = new WinRecordFruitMachineInfo();

                BeanUtils.copyProperties(resultWinInfoDTO,winRecordFruitMachineInfo);

                winRecordFruitMachineInfo.setUserId(userId);
                winRecordFruitMachineInfo.setMultiplie(resultWinInfoDTO.getMultiple());

                winRecordFruitMachineInfoRedisDao.addInfo(winRecordFruitMachineInfo);
            }

            return resultWinInfoDTO;
        }else{
            throw  new ConsumeRuntimeException("金币不足，请充值");
        }

    }

    /**
     * 抽奖账单
     * @param gold
     * @param userId
     * @param type 账单类型 16,17
     */
    private void getLuckDrawBill(int gold,long userId,int type){

        if(type == RecordTypeEnum.FRUIT_MACHINE_CONSUMPTION.state){
            //水果机抽奖消耗
            UserWallet userWallet = new UserWallet();

            userWallet.setDeposit(gold);

            userWallet.setUserId(userId);

            userWalletDao.minusDeposit(userWallet);

            // 保存消费记录
            BillDetail billDetail = new BillDetail();
            // 消费类型16 水果机消费金币
            billDetail.setRecordType(type);
            // 消费金币用户id
            billDetail.setOutUserId(userId);
            // 消费金币数
            billDetail.setSpend(gold);

            billDetailDao.insertBillDetail(billDetail);
        }else if(type == RecordTypeEnum.FRUIT_MACHINE_EARNING.state){
            //水果机抽奖赚取

            UserWallet userWallet = new UserWallet();

            userWallet.setDeposit(gold);

            userWallet.setUserId(userId);

            userWalletDao.reviseDeposit(userWallet);


            // 保存消费记录
            BillDetail billDetail = new BillDetail();
            // 消费类型17 水果赚取
            billDetail.setRecordType(type);
            // 消费金币用户id
            billDetail.setInUserId(userId);
            // 消费金币数
            billDetail.setDiamond(0);
            // 消费金币数
            billDetail.setSpend(gold);

            billDetailDao.insertBillDetail(billDetail);
        }

    }

    public List<String> getWinInfo(int size) {

        List<String> list=winRecordFruitMachineInfoRedisDao.getWinRecordList(size);

        return list;
    }

    public List<Integer> getGiftIdList() {

       List<Integer> list= fruitMachineRedisDao.getGiftIdListInfo();

       return list;
    }
}
